﻿using MediatR;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using Share;
using System.Net.NetworkInformation;
using System.Text.Json.Serialization;
using SystemLYH.API.Application.Command.ProcurementAndStorageCommand;
using SystemLYH.Domain.Dto;
using SystemLYH.Infrstructre.Interface;
using static Share.ParseAPI;
namespace SystemLYH.API.Application.Handlers.ProcurementAndStorageCommandHandler
{
    /// <summary>
    /// 采购入库显示中介者
    /// </summary>
    public class ProcurementAndStorageShowCommandHandler : IRequestHandler<ProcurementAndStorageShowCommand, PageDTO<StorageProcuredrequestDTO>>
    {
        /// <summary>
        /// 采购仓储接口
        /// </summary>
        protected readonly IProcuredrequestRepository _procuredrequestRepository;
        /// <summary>
        /// 采购申请构造函数
        /// </summary>
        /// <param name="procuredrequestRepository">采购仓储接口</param>
        public ProcurementAndStorageShowCommandHandler(IProcuredrequestRepository procuredrequestRepository)
        {
            _procuredrequestRepository = procuredrequestRepository;
        }

        public async Task<PageDTO<StorageProcuredrequestDTO>> Handle(ProcurementAndStorageShowCommand request, CancellationToken cancellationToken)
        {

            var paging = new PageDTO<StorageProcuredrequestDTO>();

            List<Storage> Storageslistto = await Storageslist();

            var list = (from proc in await _procuredrequestRepository.GetAllAsync()
                        join stor in Storageslistto
                        on proc.ProcuredNumber equals stor.DetailsNumbering
                        where proc.IsDelete == false
                        select new StorageProcuredrequestDTO
                        {
                            ProcuredId= proc.ProcuredId,
                            ProcuredNumber = proc.ProcuredNumber,
                            FeedstockNumber = proc.FeedstockNumber,
                            FeedstockName = proc.FeedstockName,
                            FeedstockNum = proc.FeedstockNum,
                            AddTime= proc.AddTime,
                            StorageId = stor.StorageId,
                            StorageNumbering = stor.StorageNumbering,
                            Status = stor.Status

                        });
            if (request.Status!=null)
            {
                list = list.Where(p => p.Status == request.Status);
            }
            if (!string.IsNullOrEmpty(request.ProcuredNumber))
            {
                list = list.Where(p => p.ProcuredNumber == request.ProcuredNumber);
            }
            if (!string.IsNullOrEmpty(request.AddTime))
            {
                var Kdate = Convert.ToDateTime(request.AddTime).Date;
                var JDate = Kdate.AddDays(1).Date;
                list = list.Where(p =>p.AddTime> Kdate& p.AddTime< JDate);
            }

            paging.pageRoot = list.Count();
            paging.pageCount = (int)Math.Ceiling((paging.pageRoot * 1.0) / request.pageSize);
            paging.plist = list.OrderBy(p => p.ProcuredId).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
            return paging;


        }

        public async Task<List<Storage>> Storageslist()
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyMCIsInNpZCI6IjE4NTk4MDY0NjEwNjkyMzQxNzYiLCJqdGkiOiI1MDhlMTQyZC1lMzQyLTQxYWMtODA0MC1kMzRhMGQ2ZTU0NTgiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTEiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi6ams5rW35rOiIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0.q2A1wd8sO7CnHgzomExiWHqWY_S_lezUIblFJ-eQKVI";
            HttpClientWrapper cilent = new HttpClientWrapper(token);
            string Storageuel = "http://47.96.140.177:8000/RawApi/api/Storages/StorageList";
            string Parse= await cilent.GetAsync(Storageuel);
            var json = JsonConvert.DeserializeObject<ApiResultList<Storage>>(Parse);
            return json.data.plist;
        }


    }

    /// <summary>
	/// 入库管理
	/// </summary>
	public class Storage : Audit
    {
        /// <summary>
        /// 入库ID
        /// </summary>
        public int StorageId { get; set; }
        /// <summary>
        /// 入库名字
        /// </summary>
        public string? StorageName { get; set; }
        /// <summary>
        /// 入库编号
        /// </summary>
        public string? StorageNumbering { get; set; }
        /// <summary>
        /// 入库日期
        /// </summary>
        public DateTime StorageDate { get; set; }
        /// <summary>
        /// 入库备注
        /// </summary>
        public string? StorageRemark { get; set; }
        /// <summary>
        /// 产品名称
        /// </summary>
        public string? DetailsName { get; set; }
        /// <summary>
        /// 产品编号
        /// </summary>
        public string? DetailsNumbering { get; set; }
        /// <summary>
        /// 规格型号
        /// </summary>
        public string? DetailsSpec { get; set; }
        /// <summary>
        /// 单位
        /// </summary>
        public string? DetailsUnit { get; set; }
        /// <summary>
        /// 批次号
        /// </summary>
        public string? DetailsNumber { get; set; }
        /// <summary>
        /// 入库数量
        /// </summary>
        public int DetailsNum { get; set; }
        /// <summary>
        /// 仓库名称
        /// </summary>
        public string? WarehouseName { get; set; }
        /// <summary>
        /// 生产日期
        /// </summary>
        public DateTime DetailsDate { get; set; }
        /// <summary>
        /// 仓库负责人
        /// </summary>
        public string? WarehouseHead { get; set; }
        public bool Status { get; set; }
        /// <summary>
        /// 删除状态
        /// </summary>
        public bool IsDelete { get; set; }
    }
}
